<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/12/15
 * Time: 10:34
 */

namespace app\admin\service\fix;


use app\api\model\Order as OrderModel;
use app\common\library\MysicLog;
use app\common\model\Order;
use app\common\model\VipCardOrder;
use app\common\service\goods\source\Factory as StockFactory;
use think\Request;


class OrderFix
{
    protected $request;
    protected $log;
    public function __construct()
    {
        $this->request = Request::instance();
        $this->log = MysicLog::instance();
    }

    public function addOrder()
    {
        $xml = $this->request->getContent();
        $data = $this->fromXml($xml);
        $deliverType = $this->request->get('deliver_type');
        $isFictitious = $this->request->get('is_ficititious');
        $orderSource = $this->request->get('order_source');
        $user = db('user')->where('open_id', $data['openid'])->field('user_id,wxapp_id')->find();
        $order = [
            'order_no' => $data['out_trade_no'],
            'total_price' => bcdiv(strval($data['total_fee']), '100', 2),
            'order_price' => bcdiv(strval($data['total_fee']), '100', 2),
            'pay_price' => bcdiv(strval($data['total_fee']), '100', 2),
            'pay_type' => 20,
            'pay_time' => strtotime($data['time_end']),
            'delivery_type' => $deliverType,
            'delivery_status' => $deliverType == 20 ? 20 : 10,
            'delivery_time' => $deliverType == 20 ? strtotime($data['time_end']) : 0,
            'receipt_status' => $deliverType == 20 ? 20 : 10,
            'order_status' => 10,
            'is_settled' => 0,
            'order_source' => $orderSource,
            'user_id' => $user['user_id'],
            'wxapp_id' => $user['wxapp_id'],
            'create_time' => strtotime($data['time_end']),
            'is_fictitious' => $isFictitious,
            'is_abouns' => 0
        ];
        $order = Order::create($order);
        $orderId = $order->getLastInsID();
        if($orderSource == 40) {
            $card = db('wxapp_card')->where(['card_real_money' => $data['total_fee'], 'wxapp_id' => $user['wxapp_id']])->find();
            $cardOrderPlan = [
                'order_id' => $orderId,
                'card_id' => $card['card_id'],
                'card_title' => $card['card_title'],
                'card_money' => $card['card_money'],
                'card_real_money' => $card['card_real_money'],
                'image_id' => $card['image_id'],
                'bonus_percent' => $card['bonus_percent'],
                'wxapp_id' => $card['wxapp_id'],
                'reward_percent' => $card['reward_percent'],
                'reward_point' => $card['reward_point'],
                'create_time' => strtotime($data['time_end']),
                'update_time' => strtotime($data['time_end']),
            ];
            VipCardOrder::create($cardOrderPlan);
        }
    }

    public function orderUpdate()
    {
        $orderNo = $this->request->post('order_no');
        $transactionId = $this->request->post('trans_id');
        $orderModel = (new OrderModel)->where('order_no', $orderNo)->find();
        if($orderModel['order_source'] == 50) {
            $time = time();
            // 整理订单信息
            $order =[
                'pay_type'       => $orderModel['pay_type']['value'],//支付方式
                'pay_status'     => 20,//付款状态(10未付款 20已付款)
                'pay_time'       => $time,//付款时间
                'order_status'   => 30,//订单状态(10进行中 20取消 21待取消 30已完成)
                'delivery_status' => 20,//发货状态(10未发货 20已发货)
                'delivery_time'   => $time,//发货时间
                'receipt_status'  =>20,//收货状态(10未收货 20已收货)
                'receipt_time'  =>$time,//收货时间
                'delivery_type'  =>30,//配送方式(10快递配送 20上门自提 30当面付)
                'is_settled'  =>1,//订单是否已结算(0未结算 1已结算)
            ];
            // $this->log('更新订单状态 - 回调transaction', $payData['transaction_id']);
            $order['transaction_id'] = $transactionId ?? '';
            return $orderModel->isUpdate(true)->save($order);
        }
        else {
            StockFactory::getFactory($orderModel['order_source'])->updateStockSales($orderModel['goods']);
            $time = time();
            // 整理订单信息
            $upData =[
                'pay_type' => $orderModel['pay_type']['value'],//支付方式
                'pay_status' => 20,//付款状态(10未付款 20已付款)
                'pay_time' => $time//付款时间
            ];
            //虚拟商品
            if($orderModel['is_fictitious']==1){
                $upData['order_status'] = 30;//订单状态(10进行中 20取消 21待取消 30已完成)
                $upData['delivery_status'] = 20;//发货状态(10未发货 20已发货)
                $upData['delivery_time'] = $time;//发货时间
                $upData['receipt_status'] = 20; //收货状态(10未收货 20已收货)
                $upData['receipt_time'] = $time;//收货时间
                $upData['delivery_type'] = 20;//配送方式(10快递配送 20上门自提)
                $upData['is_settled'] = 1;//订单是否已结算(0未结算 1已结算)
            }
            $upData['transaction_id'] = $transactionId ?? '';
            // 更新订单状态
            return $orderModel->isUpdate(true)->save($upData);
        }
    }


}